package ph.com.gs3.formalistics.model.rdao;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import ph.com.gs3.formalistics.global.constants.StarMark;
import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.HttpCommunicator;
import ph.com.gs3.formalistics.model.RemoteDataAccessObject;
import ph.com.gs3.formalistics.model.vo.application.APIResponse;
import ph.com.gs3.formalistics.model.vo.application.APIResponse.APIInvalidResponseException;

public class DocumentsRDAO extends RemoteDataAccessObject {

	public DocumentsRDAO(String server) {
		super(new HttpCommunicator(), server);
	}

	public APIResponse getFormDocumentUpdates(String formWebId, String lastUpdateDate)
	        throws RDAOCommunicationException, APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/request-list-new";

		JSONObject searchParameters = new JSONObject();

		try {
			// Add date updated comparison if available
			if (lastUpdateDate != null && !lastUpdateDate.isEmpty()) {
				JSONObject dateUpdatedConditions = new JSONObject();

				dateUpdatedConditions.put("condition", ">=");
				dateUpdatedConditions.put("compared_to_date", lastUpdateDate);

				searchParameters.put("date_updated_comparison", dateUpdatedConditions);

				// FLLogger.d(TAG, "Added search parameter, date updated should be >= "
				// + lastUpdateDate);

			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Map<String, String> requestParams = new HashMap<String, String>();
		requestParams.put("form_id", formWebId);
		requestParams.put("search_parameters", searchParameters.toString());

		return getResponse(url, requestParams);

	}

	public APIResponse submitDocumentAction(String formWebId, String documentWebId,
	        String requestData, String action) throws APIInvalidResponseException,
	        RDAOCommunicationException {

		commonValidation();

		String url;

		if ("0".equals(documentWebId.trim())) {
			url = server + "/API/create-request";
		} else {
			url = server + "/API/update-request";
		}

		Map<String, String> requestParams = new HashMap<String, String>();

		requestParams.put("form_id", formWebId);
		requestParams.put("request_id", documentWebId);
		requestParams.put("request_data", requestData);
		requestParams.put("action", action);

		FLLogger.d(TAG, "url: " + url);
		FLLogger.d(TAG, "document data: " + requestData.toString());		
		
		return getResponse(url, requestParams);

	}

	public APIResponse markDocumentStar(String formId, String requestId, StarMark star)
	        throws APIInvalidResponseException, RDAOCommunicationException {

		commonValidation();

		String url;

		if (star == StarMark.STARRED) {
			url = server + "/API/star-document";
		} else {
			url = server + "/API/unstar-document";
		}

		Map<String, String> requestParams = new HashMap<String, String>();

		requestParams.put("form_id", formId);
		requestParams.put("request_id", requestId);

		return getResponse(url, requestParams);

	}

}
